// pages/childattendancedaijie/childattendancedaijie.js
const app = getApp()
var util = require("../../utils/util.js");
const MONTHS = ['Jan.', 'Feb.', 'Mar.', 'Apr.', 'May.', 'June.', 'July.', 'Aug.', 'Sept.', 'Oct.', 'Nov.', 'Dec.'];
const qiniuUploader = require("../../utils/qiniuUploader");
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mess: {},
    relationshipList: [],
    photo: "../../images/icon_paizhao.png",
    photoList: [],
    qiniuToken: ""
  },

  photoClick: function (e) {
    console.log(e);
    var that = this
    wx.chooseImage({
      count: 1, // 默认最多上传1张
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        var tempFilePaths = res.tempFilePaths; // 返回选定照片的本地路径列表 
        that.setData({
          photoList: tempFilePaths,
          photo: tempFilePaths[0]
        })
        console.log(that.data.photoList)
      }
    })

    console.log("点击");

  },


  getQiniuToken: function (e) {

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan
    }
    wx.request({
      url: app.globalData.serverQiNiuUrl,
      data: {
        "data": JSON.stringify(data)
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          that.setData({
            qiniuToken: res.data.data.token
          })
        } else {

        }
      },
      fail: function (res) {

      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },




  btnimgClick: function (e) {
    var index = e.currentTarget.dataset.index;
    var arr = this.data.relationshipList;
    for (var i = 0; i < arr.length; i++) {
      if (i == index) {
        arr[i].status = 1
      } else {
        arr[i].status = 0
      }
    }
    this.setData({
      relationshipList: arr
    })
  },
  sendClick: function (e) {
    console.log(e);
    var that = this
    var familyidStr = ""
    var arr = this.data.relationshipList;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].status == 1) {
        familyidStr += "," + arr[i].familyID
      }
    }
    if (familyidStr.length > 0) {
      familyidStr = familyidStr.substring(1)
      if (this.data.photoList.length > 0) {
        that.sendImge(this.data.mess.UserID, familyidStr)
      } else {
        wx.showToast({
          title: '请为待接送人拍照',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }
    } else {
      wx.showToast({
        title: '请选择要发送的家长',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
    console.log("点击");

  },


  sendImge: function (kidid, familyidStr) {
    wx.showNavigationBarLoading() //在当前页面显示导航条加载动画
    wx.showLoading({ //显示 loading 提示框
      title: "处理中...",
    })
    for (var i = 0; i < this.data.photoList.length; i++) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var _this = this
      var filepath = this.data.photoList[i]
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
      qiniuUploader.upload(filepath, res => {
        console.log(res)
        var url = res.imageURL
        _this.setdaijiesong(kidid, familyidStr, url)

      }, (error) => {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.showToast({
          title: "操作失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
        return;
      }, {
        key: "jygy/" + "photo/" + app.globalData.schoolId + "/" + app.globalData.classid + "/" + timestamp + "/" + utils.formatDate(new Date()) + utils.wxuuid() + ".jpg",
        region: 'NCN',
        uptoken: _this.data.qiniuToken,
        uploadURL: 'https://up-z1.qbox.me',
        domain: app.globalData.qiniurUrl,
      })
    }

  },



  //待接送
  setdaijiesong: function (kidid, familyidStr, punchphoto) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "kidid": kidid,
      "familyidStr": familyidStr,
      "punchphoto": punchphoto,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_waitsend"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        if (res.data.status == 0) {
          wx.showToast({
            title: '操作成功',
            icon: 'none',
            duration: 1000,
            mask: true
          })
          wx.navigateBack({
            url: '../childattendanceday/childattendanceday'
          })
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.showToast({
          title: '操作失败',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        console.log('submit complete');
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mess = JSON.parse(options.mess)
    var relationshipList = mess.relationshipList
    for (var i = 0; i < relationshipList.length; i++) {
      relationshipList[i].status = 0
    }
    this.setData({
      mess: mess,
      relationshipList: relationshipList
    })

    this.getQiniuToken()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})